<?php
namespace plugins\member\controller;
use cmf\controller\ApiPluginBaseController;
use plugins\member\model\CommModel;
use plugins\member\validate\MemberValidate;
use plugins\member\model\MemberModel;
use think\Db;

class ApiIndexController extends ApiPluginBaseController
{
    const BEFORE_LUNCH_MIN = '3.92';
    const BEFORE_LUNCH_MAX = '6.16';
    const AFTER_LUNCH_MIN = '5.1';
    const AFTER_LUNCH_MAX = '7.0';

    const HIGH_PRESS_MIN = '90';
    const HIGH_PRESS_MAX = '139';
    const LOW_PRESS_MIN = '60';
    const LOW_PRESS_MAX = '89';

	//个人资料
	public function userData()
	{
		if($this->request->isPost()){
			$post = $this->request->post();
			$info = $this->getIdentity();
			$user = db('member')->field('uid',true)->where(['uid'=>$info->uid])->find();
			if($user){
				if($user['head_portrait'] != null){
					$user['head_portrait'] = zy_domain().$user['head_portrait'];
				}else{
					// $user['head_portrait'] = zy_domain()."/plugins/member/view/public/assets/user.png";
				}
				return api_json_echo(true,'查询完成',$user);
			}
			return api_json_echo(false,'查询失败，没有查询到对应用户');
		}
		return api_json_echo(false,'查询失败');
	}
	//修改登陆密码
	public function changePassword()
	{
		if($this->request->isPost()){
			$post = $this->request->post();
			$info = $this->getIdentity();
			if($post['data']['new_password'] != $post['data']['re_password']){
				return api_json_echo(false,'两次密码不相同，请重新输入');
			}
			$member = ( new MemberModel )->get(['uid'=>$info->uid,'password'=>cmf_password($post['data']['old_password'])]);
			if($member == null){
				return api_json_echo(false,'旧密码错误，请重试');
			}
			//检测通过后
			$member->password = '';
			$member->save();
			$member->password = cmf_password($post['data']['new_password']);
			$result = $member->save();
			if($result){
				return api_json_echo(true,'修改完成');
			}
			return api_json_echo(false,'修改失败');
		}
		return api_json_echo(false,'查询失败，错误的地址');
	}
    //绑定设备
    public function bandDevice()
    {
        $post = $this->request->post();
        $info = $this->getIdentity();
        $uid =$info->uid;
        if(empty($post['device'])){
            return api_json_echo(false,'请输入设备号');
        }
        if(empty($post['device_name'])){
            return api_json_echo(false,'请输入设备名称');
        }
        if(!empty($uid)){
            //判断是否已经绑定过设备
            $is_band = Db::name('member_device')->where('uid',$uid)->where('device',$post['device'])->count();
            if($is_band>0){
                return api_json_echo(false,'您已绑定过该设备');
            }
            $add['uid'] = $uid;
            $add['device'] = $post['device'];
            $add['deviceType'] = $post['deviceType'];
            $add['device_name'] = $post['device_name'];
            $result = Db::name('member_device')->insert($add);
            if($result){

                $member_model = new MemberModel();
                $member = $member_model->where('uid',$uid)->find();
                $band_data['deviceNo'] = $post['device'];
                $band_data['deviceName'] = $post['device_name'];
                $band_data['deviceType'] = $post['deviceType'];
                $band_data['realName'] = $member['user_name'];
                $band_data['certType'] = $member['idcard_type'];
                $band_data['certNum'] = $member['idcard_no'];
                $commModel = new CommModel();
                $commModel->bindingDevice($band_data);

                return api_json_echo(true,'绑定成功');
            }
            return api_json_echo(false,'绑定失败');
        }
        return api_json_echo(false,'未查询到用户');
    }

    //获取设备列表
    public function getDeviceList()
    {
        $post = $this->request->post();
        $info = $this->getIdentity();
        $uid =$info->uid;
        if(!empty($uid)){
            $list = Db::name('member_device')->where('uid',$uid)->select()->each(function ($item){

                $item['show'] = false;

                return $item;
            });
            return api_json_echo(true,'获取成功',$list);
        }
        return api_json_echo(false,'未查询到用户');
    }

    //设为默认
    public function setDefault()
    {
        $post = $this->request->post();
        $info = $this->getIdentity();
        $uid =$info->uid;
        if(empty($post['id'])){
            return api_json_echo(false,'未获取到id');
        }
        if(!empty($uid)){
            $update_one['is_default'] = 0;
            Db::name('member_device')->where('uid',$uid)->update($update_one);

            $update['is_default'] = 1;
            $list = Db::name('member_device')->where('id',$post['id'])->update($update);
            return api_json_echo(true,'设置成功',$list);
        }
        return api_json_echo(false,'未查询到用户');
    }

    //删除
    public function deletedDevice()
    {
        $post = $this->request->post();
        $info = $this->getIdentity();
        $uid =$info->uid;
        if(empty($post['id'])){
            return api_json_echo(false,'未获取到id');
        }
        if(!empty($uid)){
            Db::name('member_device')->where('id',$post['id'])->delete();
            return api_json_echo(true,'删除成功');
        }
        return api_json_echo(false,'未查询到用户');
    }

    //获取该设备的记录
    public function getDeviceLog()
    {
        $post = $this->request->post();
        $info = $this->getIdentity();
        $uid =$info->uid;
        if(!empty($uid)){
            //先查询该用户默认设备
            $info = Db::name('member_device')->where('uid',$uid)->where('is_default',1)->find();
            if(empty($info)){
                $info = Db::name('member_device')->where('uid',$uid)->find();
            }
            if(empty($info)){
                return api_json_echo(false,'请先绑定设备!');
            }
            $list = Db::name('device_log')->where('sn',$info['device'])->select()->each(function ($item){
                $item['value'] = $item['sugar'];
                $item['date'] = date('Y-m-d H:i:s',$item['time']);
                $item['time'] = date('Y-m-d H:i:s',$item['time']);
                return $item;
            });
            return api_json_echo(true,'获取成功',$list);
        }
        return api_json_echo(false,'未查询到用户');
    }

    //获取该设备血压的记录
    public function getDevicePressLog()
    {
        $post = $this->request->post();
        $info = $this->getIdentity();
        $uid =$info->uid;
        $where = ' type= 1 ';
        if(!empty($uid)){
            //先查询该用户默认设备
            $info = Db::name('member_device')->where('uid',$uid)->where('is_default',1)->find();
            if(empty($info)){
                $info = Db::name('member_device')->where('uid',$uid)->find();
            }
            if(empty($info)){
                return api_json_echo(false,'请先绑定设备!');
            }
            if(!empty($post['end_time']) && !empty($post['start_time'])){
                $where .= " and (time <= ".strtotime($post['end_time'])." and time >=".strtotime($post['start_time'])." )";
            }
            $sugar_list = Db::name('device_log')->where('sn',$info['device'])->where($where)->select();
            $total_sugar = sizeof($sugar_list);//总血压
            $high_sugar_count = 0;//高压次数
            $high_sugar_sum = 0;//高压总值
            $high_sugar_sum_count = 0;//高压总次数
            $low_sugar_count = 0;//低压次数
            $low_sugar_sum = 0;//低压总值
            $low_sugar_sum_count = 0;//低压总次数
            $pul_sum = 0;//心率总值
            $pul_sum_count = 0;//心率总次数


            $most_high_num = 0;//最高高压
            $most_low_num = 0;//最高低压
            $most_pul_num = 0;//最高心率

            $lowest_high_num = 0;//最低高压
            $lowest_low_num = 0;//最低低压
            $lowest_pul_num = 0;//最低心率

            $average_high_num = 0;//平均高压
            $average_low_num = 0;//平均低压
            $average_pul_num = 0;//平均心率


            foreach ($sugar_list as $index=>$item){

                if($index == 0){
                    $most_high_num = $item['sys'];
                    $lowest_high_num = $item['sys'];
                    $most_low_num = $item['dia'];
                    $lowest_low_num = $item['dia'];
                    $most_pul_num = $item['pul'];
                    $lowest_pul_num = $item['pul'];
                }else{
                    if($item['sys']>$most_high_num){
                        $most_high_num = $item['sys'];
                    }
                    if($item['sys']<$lowest_high_num){
                        $lowest_high_num = $item['sys'];
                    }


                    if($item['dia']>$most_low_num){
                        $most_low_num = $item['dia'];
                    }
                    if($item['dia']<$lowest_low_num){
                        $lowest_low_num = $item['dia'];
                    }

                    if($item['pul']>$most_pul_num){
                        $most_pul_num = $item['pul'];
                    }
                    if($item['pul']<$lowest_pul_num){
                        $lowest_pul_num = $item['pul'];
                    }
                }

                $high_sugar_sum += $item['sys'];
                $high_sugar_sum_count += 1;
                $low_sugar_sum += $item['dia'];
                $low_sugar_sum_count += 1;
                $pul_sum += $item['pul'];
                $pul_sum_count += 1;

                if($item['sys']>=self::HIGH_PRESS_MAX){
                    $high_sugar_count += 1;
                }elseif($item['sys']<=self::HIGH_PRESS_MIN){
                    $low_sugar_count += 1;
                }

                if($item['dia']>=self::LOW_PRESS_MAX){
                    $high_sugar_count += 1;
                }elseif($item['dia']<=self::LOW_PRESS_MIN){
                    $low_sugar_count += 1;
                }


            }
            $normal_sugar_count = $total_sugar-$high_sugar_count-$low_sugar_count;

            if($high_sugar_sum_count){
                $average_high_num = round(($high_sugar_sum/$high_sugar_sum_count),2);
            }
            if($low_sugar_sum_count){
                $average_low_num = round(($low_sugar_sum/$low_sugar_sum_count),2);
            }
            if($pul_sum_count){
                $average_pul_num = round(($pul_sum/$pul_sum_count),2);
            }

            if($total_sugar == 0){
                return api_json_echo_s(true,'获取成功',null,201);
            }else{
                $high_point = round(($high_sugar_count/$total_sugar),2)*100;
                $normal_point = round(($normal_sugar_count/$total_sugar),2)*100;
                $low_point = round(($low_sugar_count/$total_sugar),2)*100;
            }
            $res = [[
                'name'=>'高压',
                'percent'=>$high_sugar_count,
                'point'=>$high_point,
                'a'=>'1'
            ], [
                'name'=>'正常',
                'percent'=>$normal_sugar_count,
                'point'=>$normal_point,
                'a'=>'1'
            ],[
                'name'=>'低压',
                'percent'=>$low_sugar_count,
                'point'=>$low_point,
                'a'=>'1'
            ]];
            $info=[
                'most_high_num'=>$most_high_num,
                'most_low_num'=>$most_low_num,
                'most_pul_num'=>$most_pul_num,
                'lowest_high_num'=>$lowest_high_num,
                'lowest_low_num'=>$lowest_low_num,
                'lowest_pul_num'=>$lowest_pul_num,
                'average_high_num'=>$average_high_num,
                'average_low_num'=>$average_low_num,
                'average_pul_num'=>$average_pul_num,
                'high_amount'=>$high_sugar_count,
                'normal_amount'=>$normal_sugar_count,
                'low_amount'=>$low_sugar_count,
            ];
            $find_res = [
                'chart'=>$res,
                'info'=>$info
            ];
            return api_json_echo(true,'获取成功',$find_res);
        }
        return api_json_echo(false,'未查询到用户');
    }

    //获取该设备各个血压的记录
    public function getDeviceAllPressLog()
    {
        $post = $this->request->post();
        $info = $this->getIdentity();
        $uid =$info->uid;
        $where = ' type= 1 ';
        if(!empty($uid)){
            //先查询该用户默认设备
            $info = Db::name('member_device')->where('uid',$uid)->where('is_default',1)->find();
            if(empty($info)){
                $info = Db::name('member_device')->where('uid',$uid)->find();
            }
            if(empty($info)){
                return api_json_echo(false,'请先绑定设备!');
            }
            if(!empty($post['end_time']) && !empty($post['start_time'])){
                $where .= " and (time <= ".strtotime($post['end_time'])." and time >=".strtotime($post['start_time'])." )";
            }
            $sugar_list = Db::name('device_log')->where('sn',$info['device'])->where($where)->select();
            $sys_arr = [];
            foreach ($sugar_list as $item){
                $sys_item = [
                    'time'=>date('Y-m-d H:i',$item['time']),
                    'value'=>$item['sys'],
                    'type'=>'高压',
                ];
                array_push($sys_arr,$sys_item);

                $dia_item = [
                    'time'=>date('Y-m-d H:i',$item['time']),
                    'value'=>$item['dia'],
                    'type'=>'低压',
                ];
                array_push($sys_arr,$dia_item);

                $pul_item = [
                    'time'=>date('Y-m-d H:i',$item['time']),
                    'value'=>$item['pul'],
                    'type'=>'心率',
                ];
                array_push($sys_arr,$pul_item);

            }

            return api_json_echo(true,'获取成功',$sys_arr);
        }
        return api_json_echo(false,'未查询到用户');
    }

    //获取该设备各个血压的记录
    public function getDevicePressList()
    {
        $post = $this->request->post();
        $info = $this->getIdentity();
        $uid =$info->uid;
        $where = ' type= 1 ';
        if(!empty($uid)){
            //先查询该用户默认设备
            $info = Db::name('member_device')->where('uid',$uid)->where('is_default',1)->find();
            if(empty($info)){
                $info = Db::name('member_device')->where('uid',$uid)->find();
            }
            if(empty($info)){
                return api_json_echo(false,'请先绑定设备!');
            }
            if(!empty($post['end_time']) && !empty($post['start_time'])){
                $where .= " and (time <= ".strtotime($post['end_time'])." and time >=".strtotime($post['start_time'])." )";
            }
            $sugar_list = Db::name('device_log')->where('sn',$info['device'])->where($where)->select()->each(function ($item){

                $item['time'] = date('m-d H:i',$item['time']);

                return $item;
            });


            return api_json_echo(true,'获取成功',$sugar_list);
        }
        return api_json_echo(false,'未查询到用户');
    }

    //获取该设备各个血糖的记录
    public function getDeviceBloodAllList()
    {
        $post = $this->request->post();
        $info = $this->getIdentity();
        $uid =$info->uid;
        $where = ' type= 2 ';
        if(!empty($uid)){
            //先查询该用户默认设备
            $info = Db::name('member_device')->where('uid',$uid)->where('is_default',1)->find();
            if(empty($info)){
                $info = Db::name('member_device')->where('uid',$uid)->find();
            }
            if(empty($info)){
                return api_json_echo(false,'请先绑定设备!');
            }
            if(!empty($post['end_time']) && !empty($post['start_time'])){
                $where .= " and (time <= ".strtotime($post['end_time'])." and time >=".strtotime($post['start_time'])." )";
            }
            $sugar_list = Db::name('device_log')->where('sn',$info['device'])->where($where)->select()->each(function ($item){

                if($item['mealtime'] == 0){
                    $item['other'] =  $item['sugar'];
                    $item['before'] =  '-';
                    $item['after'] =  '-';
                }
                if($item['mealtime'] == 1){
                    $item['other'] =  '-';
                    $item['before'] =  $item['sugar'];
                    $item['after'] =  '-';
                }
                if($item['mealtime'] == 2){
                    $item['other'] =  '-';
                    $item['before'] = '-';
                    $item['after'] =  $item['sugar'];
                }

                $item['time'] = date('m-d H:i',$item['time']);

                return $item;
            });


            return api_json_echo(true,'获取成功',$sugar_list);
        }
        return api_json_echo(false,'未查询到用户');
    }


    //获取该设备当天的记录
    public function getDeviceLogInDay()
    {
        $post = $this->request->post();
        $mealtime = '';
        if(!empty($post['mealtime'])){
            $mealtime = $post['mealtime'];
        }
        $info = $this->getIdentity();
        $uid =$info->uid;
        $where = ' type= 2';
        if(!empty($uid)){
            //先查询该用户默认设备
            $info = Db::name('member_device')->where('uid',$uid)->where('is_default',1)->find();
            if(empty($info)){
                $info = Db::name('member_device')->where('uid',$uid)->find();
            }
            if(empty($info)){
                return api_json_echo(false,'请先绑定设备!');
            }
            if(!empty($mealtime)){
                $where .= ' and mealtime ='.$mealtime;
            }
            $sugar_list = Db::name('device_log')->where('sn',$info['device'])->whereTime('time', 'today')->where($where)->select();
            $total_sugar = sizeof($sugar_list);//总次数
            $high_sugar_count = 0;//高血糖次数
            $high_sugar_sum = 0;//高血糖总值
            $low_sugar_sum = 0;//低血糖总值
            $low_sugar_count = 0;//低血糖次数
            $normal_sugar_sum = 0;//正常血糖总值
            $normal_sugar_count = 0;//正常血糖次数
            foreach ($sugar_list as $item){

                if($item['mealtime'] == 1){
                    if($item['sugar']>self::BEFORE_LUNCH_MAX){//餐前高血糖
                        $high_sugar_sum += $item['sugar'];
                        $high_sugar_count += 1;
                    }elseif ($item['sugar']<self::BEFORE_LUNCH_MIN){//餐前低血糖
                        $low_sugar_sum += $item['sugar'];
                        $low_sugar_count += 1;
                    }else{//正常血糖
                        $normal_sugar_sum += $item['sugar'];
                        $normal_sugar_count += 1;
                    }
                }elseif($item['mealtime'] == 2){
                    if($item['sugar']>self::AFTER_LUNCH_MAX){//餐后高血糖
                        $high_sugar_sum += $item['sugar'];
                        $high_sugar_count += 1;
                    }elseif ($item['sugar']<self::AFTER_LUNCH_MIN){//餐后低血糖
                        $low_sugar_sum += $item['sugar'];
                        $low_sugar_count += 1;
                    }else{//正常血糖
                        $normal_sugar_sum += $item['sugar'];
                        $normal_sugar_count += 1;
                    }
                }else{
                    $normal_sugar_sum += $item['sugar'];
                    $normal_sugar_count += 1;
                }
            }
            if($total_sugar == 0){
                return api_json_echo_s(true,'获取成功',null,201);
            }else{
                $high_point = round(($high_sugar_count/$total_sugar),2)*100;
                $normal_point = round(($normal_sugar_count/$total_sugar),2)*100;
                $low_point = round(($low_sugar_count/$total_sugar),2)*100;
            }
            $res = [[
                'name'=>'高血糖',
                'percent'=>$high_sugar_count,
                'point'=>$high_point,
                'a'=>'1'
            ], [
                'name'=>'正常',
                'percent'=>$normal_sugar_count,
                'point'=>$normal_point,
                'a'=>'1'
            ],[
                'name'=>'低血糖',
                'percent'=>$low_sugar_count,
                'point'=>$low_point,
                'a'=>'1'
            ]];
            return api_json_echo(true,'获取成功',$res);
        }
        return api_json_echo(false,'未查询到用户');
    }

    //根据日期范围获取该设备记录===折线图
    public function getDeviceLogByDays()
    {
        $post = $this->request->post();
        $mealtime = '';
        if(!empty($post['mealtime'])){
            $mealtime = $post['mealtime'];
        }
        $info = $this->getIdentity();
        $uid =$info->uid;
        $where = ' type= 2';
        if(!empty($uid)){
            //先查询该用户默认设备
            $info = Db::name('member_device')->where('uid',$uid)->where('is_default',1)->find();
            if(empty($info)){
                $info = Db::name('member_device')->where('uid',$uid)->find();
            }
            if(empty($info)){
                return api_json_echo(false,'请先绑定设备!');
            }
            if(!empty($mealtime)){
                $where .= ' and mealtime ='.$mealtime;
            }
            if(!empty($post['end_time']) && !empty($post['start_time'])){
                $where .= " and (time <= ".strtotime($post['end_time'])." and time >=".strtotime($post['start_time'])." )";
            }
            $sugar_list = Db::name('device_log')->where('sn',$info['device'])->where($where)->select()->each(function ($item){
                $item['tag'] = 0;
                $item['value'] = $item['sugar'];
                $item['date'] = date('Y-m-d H:i',$item['time']);
                if($item['mealtime'] == 1){
                    if($item['sugar']>self::BEFORE_LUNCH_MAX){//餐前高血糖
                        $item['tag'] = 1;
                    }elseif ($item['sugar']<self::BEFORE_LUNCH_MIN){//餐前低血糖
                        $item['tag'] = 2;
                    }
                }elseif($item['mealtime'] == 2){
                    if($item['sugar']>self::AFTER_LUNCH_MAX){//餐后高血糖
                        $item['tag'] = 1;
                    }elseif ($item['sugar']<self::AFTER_LUNCH_MIN){//餐后低血糖
                        $item['tag'] = 2;
                    }
                }
                return $item;
            });
            return api_json_echo(true,'获取成功',$sugar_list);
        }
        return api_json_echo(false,'未查询到用户');
    }

	//修改所在城市
	public function changeCity()
	{
		if($this->request->isGet()){
			$get = $this->request->get();
			$info = $this->getIdentity();
			$member = ( new MemberModel )->get(['uid'=>$info->uid]);
			if($member){
				$city = $member->city;

				return api_json_echo(true,'查询完成',$city);
			}
			return api_json_echo(false,'未查询到用户');
		}
		if($this->request->isPost()){
			$post = $this->request->post();
			$info = $this->getIdentity();
			$member = ( new MemberModel )->get(['uid'=>$info->uid]);
			if($member){
				$member->city = $post['city'];
				$result = $member->save();
				if($result){
					return api_json_echo(true,'修改完成');
				}
				return api_json_echo(false,'修改失败');
			}
			return api_json_echo(false,'未查询到用户');
		}
	}
	//修改出生日期
	public function changeBirthday()
	{
		if($this->request->isGet()){
			$get = $this->request->get();
			$info = $this->getIdentity();
			$member = ( new MemberModel )->get(['uid'=>$info->uid]);
			if($member){
				$birthday = $member->birthday;
				return api_json_echo(true,'查询完成',$birthday);
			}
			return api_json_echo(false,'未查询到用户');
		}
		if($this->request->isPost()){
			$post = $this->request->post();
			$info = $this->getIdentity();
			$member = ( new MemberModel )->get(['uid'=>$info->uid]);
			if($member){
				$member->birthday = $post['birthday'];
				$result = $member->save();
				if($result){
					return api_json_echo(true,'修改完成');
				}
				return api_json_echo(false,'修改失败');
			}
			return api_json_echo(false,'未查询到用户');
		}
	}
	//修改个人简介
	public function changeIndividual()
	{
		if($this->request->isGet()){
			$get = $this->request->get();
			$info = $this->getIdentity();
			$member = ( new MemberModel )->get(['uid'=>$info->uid]);
			if($member){
				$individual = $member->individual;
				return api_json_echo(true,'查询完成',$individual);
			}
			return api_json_echo(false,'未查询到用户');
		}
		if($this->request->isPost()){
			$post = $this->request->post();
			$info = $this->getIdentity();
			$member = ( new MemberModel )->get(['uid'=>$info->uid]);
			if($member){
				$member->individual = $post['individual'];
				$result = $member->save();
				if($result){
					return api_json_echo(true,'修改完成');
				}
				return api_json_echo(false,'修改失败');
			}
			return api_json_echo(false,'未查询到用户');
		}
	}
	//修改头像
	public function changeHeadImg()
	{
		if($this->request->isGet()){
			$get = $this->request->get();
			$info = $this->getIdentity();
			$member = ( new MemberModel )->get(['uid'=>$info->uid]);
			if($member){
				if($member->head_portrait != null){
					$head_portrait = zy_domain().$member->head_portrait;
				}else{
					$head_portrait = zy_domain()."/plugins/member/view/public/assets/user.png";
				}

				return api_json_echo(true,'查询完成',$head_portrait);
			}
			return api_json_echo(false,'未查询到用户');
		}
		if($this->request->isPost()){
			$post = $this->request->post();
			$info = $this->getIdentity();
			$member = ( new MemberModel )->get(['uid'=>$info->uid]);
			if($member){
				$image = $this->base64_image_content($post['head_portrait']);
				$member->head_portrait = $image;
				$result = $member->save();
				if($result){
					return api_json_echo(true,'修改完成',zy_domain().$member->head_portrait);
				}
				return api_json_echo(false,'修改失败');
			}
			return api_json_echo(false,'未查询到用户');
		}
	}
	//修改性别
	public function changeSex()
	{
		if($this->request->isGet()){
			$get = $this->request->get();
			$info = $this->getIdentity();
			$member = ( new MemberModel )->get(['uid'=>$info->uid]);
			if($member){
				$sex = $member->sex;
				return api_json_echo(true,'查询完成',$sex);
			}
			return api_json_echo(false,'未查询到用户');
		}
		if($this->request->isPost()){
			$post = $this->request->post();
			$info = $this->getIdentity();
			$member = ( new MemberModel )->get(['uid'=>$info->uid]);
			if($member){
				$member->sex = $post['sex'];
				$result = $member->save();
				if($result){
					return api_json_echo(true,'修改完成');
				}
				return api_json_echo(false,'修改失败');
			}
			return api_json_echo(false,'未查询到用户');
		}
	}
	//修改昵称
	public function changeNickName()
	{
		if($this->request->isPost()){
			$post = $this->request->post();
			$info = $this->getIdentity();
			$member = ( new MemberModel )->get(['uid'=>$info->uid]);
			$member->nick_name = '';
			$member->save();
			$member->nick_name = $post['nick_name'];
			$result = $member->save();
			if($result){
				return api_json_echo(true,'修改完成');
			}
			return api_json_echo(false,'修改失败');
		}
		return api_json_echo(false,'查询失败');
	}
	//验证手机
	public function checkPassword()
	{
	}
    /**
     * [base64_image_content base64编码字符串 保存并生成图片链接]
     * @param  [type] $image [base64编码字符串]
     * @return [type]        [description]
     */
    private function base64_image_content($image){
        if($image == base64_encode(base64_decode($image))){
            return $image;
        }
        $sui=mt_rand(10000,99999);
        $imageName = date('Ymd').'/'.$sui.date('His').".png"; //设置生成的图片名字
        if (strstr($image,",")){
            $image = explode(',',$image);
            $image = $image[1];
        }
        //获取网站路径
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
        $wzurl = $http_type.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
        $wzurl= substr($wzurl,0,strrpos($wzurl,'/'));
        $this->uploadpath = ROOT_PATH.'public/plugins/member/view/public/assets/images/default';
        $path =  $this->uploadpath;
        // $path = "./upload/pingjia"; // 设置图片保存路径
        if (!is_dir($path.'/'.date('Ymd'))){ //判断目录是否存在 不存在就创建 并赋予777权限
            mkdir($path.'/'.date('Ymd'),0777,true);
        }
        $imageSrc= $path."/". $imageName; //图片名字
        $r = file_put_contents($imageSrc, base64_decode($image));//返回的是字节数
        if (!$r) {
            return zy_json_echo(error,'图片生成失败',null,-1);
        }else{
            // $pic = explode('.',$imageSrc,2)['1'];
            return "/plugins/member/view/public/assets/images/default/".$imageName;
        }
    }
	//登录后发短信
	public function sendCode()
	{
		$info = $this->getIdentity();
		$member = ( new MemberModel )->get(['uid'=>$info->uid]);
		//验证码生成
		$code = cmf_get_verification_code($member->phone, 6);
		if($code == false){
			return api_json_echo( false , '发送失败，今日发送次数已达到15次上限' , $code );
		}
		//验证码发送
		$data = [
            'mb_name' => 'verify_code', //模板标识,项目中可自定义,定义以后到短信配置做相应的模板配置, 但是在写标准基础模块和插件时必须按照规范书写,方便复用
            'mobile' => $member->phone,//手机号
            'content' => [  //发送内容,用于存放模板变量, 默认可以设置 [] 空数组,
                'code' => $code
            ]
        ];
        $return = cmf_sms_send( $data );
        if($return['status'] == true){
	 		//更新验证码发送日志
			$result = cmf_verification_code_log($member->phone, $code, (time()+300));
			if($result){
				return api_json_echo( true , '发送成功');
			}else{
				return api_json_echo( false , '发送失败');
			}
        }else{
        	return api_json_echo( false , '发送失败');
        }
	}
	//登陆后检查短信验证码
	public function checkCode()
	{
		$post = $this->request->post();
		$info = $this->getIdentity();
		$member = ( new MemberModel )->get(['uid'=>$info->uid]);
		//手机验证码检测
		$check_code = cmf_check_verification_code($member->phone, $post['code'],true);
		if($check_code != ''){
			return api_json_echo( false , $check_code);
		}
		return api_json_echo( true , '验证成功');
	}
	//更换手机号
	public function changePhone()
	{
		$post = $this->request->post();
		$info = $this->getIdentity();

		$member = ( new MemberModel)->get(['uid'=>$info->uid]);
		if(!preg_match("/^1[34578]\d{9}$/", $post['phone'])){
			return api_json_echo( false , '请输入正确的手机号' );
		}
		//验证手机验证码
		$check_code = cmf_check_verification_code($post['phone'], $post['code'],true);
		if($check_code != ''){
			$result = $member->save(['phone'=>$post['phone']]);
			if($result){
				return api_json_echo(true,'更换手机号成功!');
			}
			return api_json_echo(false,'更换失败请重试');
		}
		return api_json_echo(false,'验证码输入错误请重试');
	}
}
